<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title></title>
	<link rel="stylesheet" href="./Public/Admin/Bootstrap/Css/bootstrap.min.css" />
	<link rel="stylesheet" href="./Public/Admin/Css/common.css" />

	<script type="text/javascript" src="Public/Admin/uploadify/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="Public/Admin/uploadify/jquery.uploadify.min.js"></script>
    <link rel="stylesheet" type="text/css" href="Public/Admin/uploadify/uploadify.css">

    <script type="text/javascript" charset="utf-8" src="./Public/Admin/ueditor1_4_3/ueditor.config.js"></script>
	<script type="text/javascript" charset="utf-8" src="./Public/Admin/ueditor1_4_3/ueditor.all.min.js"> </script>
	<script type="text/javascript" charset="utf-8" src="./Public/Admin/ueditor1_4_3/lang/zh-cn/zh-cn.js"></script>

	<style type="text/css">
		/*去掉uploadify上传按钮的边框*/
	    .uploadify-button {
	        background-color: transparent;
	        border: none;
	        padding: 0;
	    }
	    .uploadify:hover .uploadify-button {
	        background-color: transparent;
    	}
    	.hide{
    		display: none;
    	}
    	body{
    		margin-bottom: 100px;
    	}
	</style>


	<script type="text/javascript">
		$(function(){

			$('select[name=cid]').change(function(){
				//获得选中的option的tid
				var tid = $('option:selected').attr('tid');
//				console.log(tid);
				$.ajax({
					type:"post",
					url:"{{U('Goods/getAttr')}}",
					data:{t : tid},
					//确定返回数据类型
					dataType:'json',
					success:function(phpData){
						console.log(phpData);
						var attr = '';
						var spec = '';
						$.each(phpData, function(k,v) {
							//如果是属性
							if(v.class == 0){
								attr += '<tr class="info">';
								attr += '<td>'+v.taname+'</td>';
								attr += '<td>';
								attr += '<select name="attr['+v.taid+']" id="">';
								attr += '<option value="">请选择</option>';
									 $.each(v.tavalue, function(kk,vv) {
										attr += '<option value="'+vv+'">'+vv+'</option>';
									 });
								attr += '</select>';
								attr += '</td></tr>';

							}else{//如果是规格
								spec += '<tr class="info">';
								spec += '<td>'+v.taname+'</td>';
								spec += '<td>';
								spec += '<select name="spec[value]['+v.taid+'][]" id="">';
								spec += '<option value="">请选择</option>';
									$.each(v.tavalue, function(kk,vv) {
										spec += '<option value="'+vv+'">'+vv+'</option>';
									});
								spec += '</select></td>';
								spec += '<td>附加价格：<input type="text" name="spec[added]['+v.taid+'][]" id=""/></td>';
								spec += '<td>';
								spec += '<a href="javascript:;" class="btn btn-primary addSpec">添加规格</a>';
								spec += '</td></tr>';
							}

						});

						$('#attr').html(attr);
						$('#spec').html(spec);
					}
				});
			})
			//点击添加规格
			$('.addSpec').live('click',function(){
				//找到当前的父级的tr
				var trObj = $(this).parents('tr');
				//克隆一个
				var tr = trObj.clone();
				tr.find('.addSpec').html('删除规格').removeClass('btn-primary addSpec').addClass('btn-info removeSpec');
				//再插入到当前父级的下面
				trObj.after(tr);

			})
			$('.removeSpec').live('click',function(){
				$(this).parents('tr').remove();
			})
		})
	</script>
	</script>


</head>
<body>
	<div class="container-fluid">
	<div class="row-fluid">
	<div class="span12">
	<form action="" method='post'>
		<fieldset>
			<legend>编辑商品</legend>
			<table class='table table-bordered table-hover'>
				<thead>
					<tr>
						<th colspan="2" class="btn btn-primary">基本信息</th>
					</tr>
				</thead>
				<tbody>
					<tr class="info">
						<td>所属分类</td>
						<td>
							<select name="cid">
								<option value="0">-请选择-</option>
								{{foreach from="$cateData" value="$v"}}

									<option value="{{$v['cid']}}" tid={{$v['type_tid']}} <?php if($oldData['category_cid'] == $v['cid']): ?>selected<?php endif ?> >{{$v['_name']}}</option>

								{{endforeach}}
							</select>
						</td>
					</tr>

					<tr class="info">
						<td>商品名称</td>
						<td>
							<input type="text" name='gname' value="{{$oldData['gname']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>库存</td>
						<td>
							<input type="text" name='stock' value="{{$oldData['stock']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>单位</td>
						<td>
							<input type="text" name='unit' value="{{$oldData['unit']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>市价场</td>
						<td>
							<input type="text" name='marketprice' value="{{$oldData['marketprice']}}"/>
						</td>
					</tr>
					<tr class="info">
						<td>商城价</td>
						<td>
							<input type="text" name='shopprice' value="{{$oldData['shopprice']}}"/>
						</td>
					</tr>

				</tbody>
			</table>

			<p class="btn btn-primary">商品属性</p>
			<table class="table table-bordered table-hover" id='attr' class="info">
				{{foreach from="$data" value="$v"}}
					{{if value="$v['class'] eq 0"}}
						<tr class="info">
							<td>{{$v['taname']}}</td>
							<td>
								<select name="">
									<option value="">请选择</option>
									{{foreach from="$v['tavalue']" value="$value"}}
										<option value="{{$value}}" {{if value="$v['gavalue'] eq $value"}} selected {{endif}}>{{$value}}</option>
										<!--<option value="{{$value}}" {{if value="$v['gtvalue'] eq $value"}}selected{{endif}} >{{$value}}</option>-->
									{{endforeach}}
								</select>
							</td>
						</tr>
					{{endif}}
				{{endforeach}}


			</table>
			<p class="btn btn-primary">商品规格</p>
			<?php
				//定义空数组
				$taid=[];
			 ?>
			<table class="table table-bordered table-hover" id='spec'>
				{{foreach from="$data" value="$v"}}
					{{if value="$v['class'] eq 1"}}
						<?php
							//往空数组压内容
							$taid[]=$v['taid'];
						 ?>
						<tr class="info">
							<td>{{$v['taname']}}</td>
							<td>
								<select name="">
									<option value="">请选择</option>
									{{foreach from="$v['tavalue']" value="$value"}}
										<option value="{{$value}}" {{if value="$v['gavalue'] eq $value"}} selected {{endif}}>{{$value}}</option>
									{{endforeach}}
								</select>
							</td>
							<td>

								附加价格: <input type="text" name="added" value="{{$v['added']}}">
							</td>

							<td>
								<a href="javascript:;" class="btn btn-info removeSpec goodsAttr{{$v['taid']}}">删除规格</a>
							</td>
						</tr>
					{{endif}}
				{{endforeach}}
			</table>
			<?php
				//去重
				$taid=array_unique($taid);
//				p($taid);

			 ?>
			 <script type="text/javascript">
			 	var taid='<?php echo json_encode($taid)?>'
			 	//把js字符串转为json对象
			 	taid=$.parseJSON(taid);
			 	//打印
//				console.log(taid);

			 	//循环json
			 	$.each(taid, function(k,v) {
			 		$('.goodsAttr'+v).first().removeClass('removeSpec btn-info').addClass('addSpec btn-primary').html('增加规格');
			 	});
			 </script>

			<table class='table table-bordered'>
				<tr>
					<th colspan="3" class="btn btn-primary">列表图</th>
				</tr>
				<tr class="info">
					<td>上传图片</td>
					<td>
						<div lab="uploadFile">
	<!-- file表单 -->
    <input id="f" type="file" multiple="true">
    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
    <script type="text/javascript">
        $(function() {
            $('#f').uploadify({
                'formData'     : {//POST数据
                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
                },
                'fileTypeExts' : '*.jpg;*.png',
                'swf'      : '{{__PUBLIC__}}/Admin/uploadify/uploadify.swf',
                'uploader' : '{{U('upload')}}',//指定服务器上传的方法
                'buttonText':'选择文件',
                'fileSizeLimit' : '2000KB',
                'uploadLimit' : 3,//上传文件数
                'width':65,
                'height':25,
                'successTimeout':10,//等待服务器响应时间
                'removeTimeout' : 0.2,//成功显示时间
                'onUploadSuccess' : function(file, data, response) {
                    //把php返回的数据转为json
                    data=$.parseJSON(data);
                    //获得图片地址
                    var imageUrl = data.url;
                    var li="<li>";
                    li += "<img src='"+imageUrl+"'width=200/>";
                    li += "<input type='hidden' name='pic'  '"+data.path+"'/><a href='javascript:;' id='self-close'>X</a>";
                    li += "</li>";
                    $("#uploadLists ul").prepend(li);
                }
            });

			//关闭图片
            var i = 1;
            $('#self-close').live('click',function(){
                $(this).parent('li').remove();
                i++;
                $('#f').uploadify('settings','uploadLimit',i);
            })
        });
    </script>
    <div id="uploadLists">
        <ul>
            <li>
                <img src="{{$oldData['pic']}}" alt="">
                <input type='hidden' name='pic'  '"+data.path+"'/>
                <a href='javascript:;' id='self-close'>X</a>
            </li>
        </ul>
    </div>
</div>
					</td>
					<td id='pic-list'></td>
				</tr>
			</table>

			<table class='table table-bordered'>
				<tr>
					<th colspan="3" class="btn btn-primary">商品图册</th>

				</tr>
				<tr class="info">
					<td>上传图片</td>

					<td>
						<div lab="uploadFiles">

	<!-- file表单 -->
    <input id="t" type="file" multiple="true">
    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
    <script type="text/javascript">
        $(function() {
            $('#t').uploadify({
                'formData'     : {//POST数据
                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
                },
                'fileTypeExts' : '*.jpg;*.png',
                'swf'      : '{{__PUBLIC__}}/Admin/uploadify/uploadify.swf',
                'uploader' : '{{U('upload')}}',//指定服务器上传的方法
                'buttonText':'选择文件',
                'fileSizeLimit' : '2000KB',
                'uploadLimit' : 1,//上传文件数
                'width':65,
                'height':25,
                'successTimeout':10,//等待服务器响应时间
                'removeTimeout' : 0.2,//成功显示时间
                'onUploadSuccess' : function(file, data, response) {
                    //把php返回的数据转为json
                    data=$.parseJSON(data);
                    //获得图片地址
                    var imageUrl = data.url;
                    var li="<li>";
                    li += "<img src='"+imageUrl+"'width=200/>";
                    li += "<input type='hidden' name='pic'  '"+data.path+"'/><a href='javascript:;' id='self-close'>X</a>";
                    li += "</li>";
                    $("#uploadList ul").prepend(li);
                }
            });

			//关闭图片
            var i = 1;
            $('#self-close').live('click',function(){
                $(this).parent('li').remove();
                i++;
                $('#t').uploadify('settings','uploadLimit',i);
            })
        });
    </script>
    <div id="uploadList">
        <ul>
            <li>
                <img src="{{$oldData['smallpic']}}" alt="">
                <input type='hidden' name='pic'  '"+data.path+"'/>
                <a href='javascript:;' id='self-close'>X</a>
            </li>
        </ul>
    </div>
</div>
					</td>
					<td id='photo-list'></td>
				</tr>
			</table>



	<table class='table table-bordered'>
		<tr>
			<th colspan="3" class="btn btn-primary">商品详情图册</th>
		</tr>
		<tr class="info">
			<td>上传图片</td>
			<td>
				<div lab="uploadFiles">
					<!-- file表单 -->
					<input id="s" type="file" multiple="true">
					<span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
					<script type="text/javascript">
						$(function() {
							$('#s').uploadify({
								'formData'     : {//POST数据
									'<?php echo session_name();?>' : '<?php echo session_id();?>',
								},
								'fileTypeExts' : '*.jpg;*.png',
								'swf'      : '{{__PUBLIC__}}/Admin/uploadify/uploadify.swf',
								'uploader' : '{{U('upload')}}',//指定服务器上传的方法
								'buttonText':'选择文件',
								'fileSizeLimit' : '2000KB',
								'uploadLimit' : 30,//上传文件数
								'width':65,
								'height':25,
								'successTimeout':10,//等待服务器响应时间
								'removeTimeout' : 0.2,//成功显示时间
								'onUploadSuccess' : function(file, data, response) {
									//把php返回的数据转为json
									data=$.parseJSON(data);
									//获得图片地址
									var imageUrl = data.url;
									var li="<li>";
									li += "<img src='"+imageUrl+"'width=200/>";
									li += "<input type='hidden' name='picintro[]' value='"+data.path+"'/><a href='javascript:;' id='self-close'>X</a>";
									li += "</li>";
									$("#uploadListl ul").prepend(li);
								}
							});

							//关闭图片
							var i = 1;
							$('#self-close').live('click',function(){
								$(this).parent('li').remove();
								i++;
								$('#s').uploadify('settings','uploadLimit',i);
							})
						});
					</script>
					<div id="uploadListl">
						<ul>
							<li>
								<img src="{{$oldData['picintro']}}" alt="">
								<input type='hidden' name='pic'  '"+data.path+"'/>
								<a href='javascript:;' id='self-close'>X</a>
							</li>
						</ul>
					</div>
				</div>
			</td>
			<td id='photo-list'></td>
		</tr>
	</table>

			<table class='table'>
				<tr class="next_show">
					<th class="btn btn-primary" >商品详细</th>
				</tr>
			<!--调用百度编辑器-->
			<script id="editor" type="text/plain" style="width:100%;height:500px;" name="intro" >{{$oldData['intro']}}</script>
		    <script>
		        var ue = UE.getEditor('editor');
		    </script>

			</table>


			<input type="hidden" name='gid' value="{{$_GET['gid']}}" />
			<input type="submit" value="确认添加" class="btn btn-primary btn-block btn-large" />
		</fieldset>
	</form>

	</div>
	</div>
	</div>
</body>
</html>